package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.Student;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.Impl.StudentServiceImpl;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author 95741
 * @version 1.0
 * @date 2020/12/30 20:37
 */


@WebServlet("/StudentController")
@Controller
public class StudentController extends BaseController {

    private StudentService service;

    @Autowired
    public void setService(StudentService service) {
        this.service = service;
    }

    public void find(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer pageNum = Integer.parseInt(request.getParameter("PageNum"));
        PageHelper.startPage(pageNum, 3);
        List<Student> allStudent = service.findAllStudent();
        PageInfo<Student> teacherVoPageInfo = new PageInfo<>(allStudent);
        String jsonString = JSON.toJSONString(teacherVoPageInfo);
        response.getWriter().write(jsonString);
    }

    /*
    *  "name": that.name,
     "id": that.nameid,*/
    public void findByID(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer pageNum = Integer.parseInt(request.getParameter("PageNum"));
        String name = request.getParameter("name");
        PageHelper.startPage(pageNum, 3);
        List<Student> allStudent = service.findStudent(new Student(null, name, null));
        PageInfo<Student> teacherVoPageInfo = new PageInfo<>(allStudent);
        String jsonString = JSON.toJSONString(teacherVoPageInfo);
        response.getWriter().write(jsonString);
    }

    /*
    * "name": that.addname,
                        "money": that.addmoeny*/

    public void insertStudent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        Integer money = Integer.parseInt(request.getParameter("money"));
        Student student = new Student(null, name, money);
        int i = service.insertStudent(student);
        response.getWriter().write("添加成功");
    }

    public void deleteByid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        if (id != null && !id.equals("")) {
            id = id.substring(0, id.length() - 1);
        }
        int delete = service.delete(id);
        response.getWriter().write("删除成功");
    }

    public void findstudentbyid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Student findbyid = service.findbyid(Integer.parseInt(id));
        String jsonString = JSON.toJSONString(findbyid);
        response.getWriter().write(jsonString);
    }


    /*
    *
     "id": that.editid,
      "name": that.editname,
     "money": that.editmoney
    * */

    public void submitedit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String money = request.getParameter("money");

        Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(money));
        int i = service.updateByPrimaryKey(student);
        response.getWriter().write("更新成功");
    }

    public void changename(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");


        String s = service.selectNameByid(Integer.parseInt(id));
        response.getWriter().write(s);
    }

    public void changeid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");


        Integer integer = service.selectIdByName(name);
        System.out.println(integer.toString());
        response.getWriter().write(integer.toString());
    }
    //  "id": that.transferid,
    //   "id1": that.transferid1,
    //   "name": that.transfername,
    //   "money": that.transfermoney,
    //   "moeny1": that.transfermoney1

    public void submittransfer(HttpServletRequest request, HttpServletResponse response) {
        String newid = request.getParameter("id");
        String newname = request.getParameter("name");
        String newmoney = request.getParameter("money");

        Integer newid2;
        if (newid == null || newid == "") {
            newid2 = null;
        } else {
            newid2 = Integer.parseInt(newid);
        }

        String oldid = request.getParameter("id1");
        String moeny1 = request.getParameter("moeny1");

        Student newStu = new Student(newid2, newname, Integer.parseInt(newmoney));

        Student oldStu = new Student(Integer.parseInt(oldid), null, -Integer.parseInt(newmoney));
        try {
            service.transferMoney(newStu, oldStu, Integer.parseInt(moeny1), Integer.parseInt(newmoney));
            response.getWriter().write("转账成功成功");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                response.getWriter().write("转账失败");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }


}
